//跳转的登录页面url
import rxAjax from '@/assets/js/ajax.js';

export const LOGIN_URL = "/oauth/authorize";
export const client_id = "webApp"; //应用ID对应oauth里面的client_id
export const response_type = "code";
export const state = "jpaas";
export const MS_ = 12 * 60 * 60 * 1000;

export const FULL_CHARTER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopgrstuvwxyz';

export const getState = function () {
	let state = '';
	for (let i = 0; i < 6; i++) {
		state += FULL_CHARTER[Math.floor(Math.random() * 52)];
	}
	return state;
}

/**
 * 获取url参数
 */
export const getQueryVariable = function (variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) { return pair[1]; }
	}
	return '';
}

//构建调整url
export const getAuthorizeUri = function (state,client_id,callback) {
	const url = "/api-user/user/org/token/getByClientId?clientId=" + client_id;
	rxAjax.get(url)
	.then(res => {
		var data = res.data;
		var authorizeUri = data.serverAddress + LOGIN_URL + '?client_id=' + data.clientId + '&redirect_uri=' + data.redirect_uri + '&response_type=' + response_type + '&scope=' + data.scope + '&state=' + state;
		if(callback) {
			callback(authorizeUri,data);
		}
	});
}